import moment from 'moment'
import { ControlType } from '../../config/Constant'
import { isObject, isBoolean } from '@core'

/**
 * 处理联动数据，从表单数据到控件值
 * @param {*} initialValues
 * @param {*} fields
 * @returns
 */
const extendInitialValues = (initialValues, fields) => {
  const values = { ...initialValues }

  fields?.forEach((field) => {
    if (field.linkageFields && field.linkageFields.length > 0) {
      // const controlValues = [field.linkageFields.length]
      const controlValues = Array(2).fill(null)
      const NUMBER_RANGE = {}
      field.linkageFields?.forEach((name, index) => {
        switch (field.controlType) {
          case ControlType.DATE_RANGE_PICKER:
            controlValues[index] = values[name] ? moment(values[name]) : null
            break
          case ControlType.INPUT_NUMBER_RANGE:
            NUMBER_RANGE[name] = values[name] ? values[name] : null
            break
          default:
            break
        }
      })
      if(field.controlType === ControlType.INPUT_NUMBER_RANGE){
        values[field.name] = NUMBER_RANGE
      }
      if(field.controlType === ControlType.DATE_RANGE_PICKER) {
        values[field.name] = controlValues.filter(_ => _).length ? controlValues : null
      }
      
    }
  })
  return values
}

/**
 * 处理联动数据，从控件值到表单数据
 * @param {*} changedValues
 * @param {*} fields
 * @returns
 */
const extendChangedValues = (changedValues, fields) => {
  const values = { ...changedValues }

  const linkageValues = {}
  fields?.forEach((field) => {
    const controlValues = changedValues[field.name]

    if(field.controlType === ControlType.DATE_PICKER){
      values[field.name] = controlValues ? getFormatDate(field, controlValues) : null
    }

    if (field.linkageFields && field.linkageFields.length > 0) {
      //&&controlValues
      field.linkageFields?.forEach((name, index) => {
        switch (field.controlType) {
          case ControlType.DATE_RANGE_PICKER:
            values[name] = controlValues?.[index] ? getFormatDate(field, controlValues[index]) : null
            linkageValues[name] = controlValues?.[index] ? getFormatDate(field, controlValues[index]) : null
            break
          case ControlType.INPUT_NUMBER_RANGE:
            values[name] = controlValues ? controlValues[name] : 0
            linkageValues[name] = controlValues ? controlValues[name] : 0
            break
          default:
            break
        }
      })
      delete values[field.name]
    }
  })

  return { values, linkageValues }
}

// 获取格式化的日期
const getFormatDate = (field, date) => {
  const {showTime, picker, format} = field

  switch (picker) {
    case 'week':
      return moment(date).format('YYYY-W周');
    case 'month':
      return moment(date).format('YYYY-MM');
    case 'quarter':
      return moment(date).format('YYYY-qQ').toUpperCase();
    case 'year':
      return moment(date).format('YYYY');
    default: 
      if (isObject(showTime)) {
        const {format: showTimeFormat} = showTime
        switch (showTimeFormat) {
          case 'HH':
            return moment(date).startOf('hour').format(format ? format : 'YYYY-MM-DD HH:mm');
          case 'HH:mm':
            return moment(date).startOf('minute').format(format ? format : 'YYYY-MM-DD HH:mm');
          default:
            return moment(date).format(format ? format : 'YYYY-MM-DD HH:mm:ss');
        }
      } else {
        return moment(date).format(showTime ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD');
      }
      
  
  }

}

export { extendInitialValues, extendChangedValues }
